﻿Imports System.Windows.Forms

Public Class ClassView
    Inherits TreeView

    Public Sub New()
        Font = New Font("Segoe UI", 9.5)
        Nodes.Add("Loading...")
    End Sub

    Private Sub ClassView_KeyDown(ByVal sender As System.Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Enter Then
            OpenSelectedNodeEditorTab()
        End If
    End Sub

    Private Sub ClassView_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
        OpenSelectedNodeEditorTab()
    End Sub

    Private Sub OpenSelectedNodeEditorTab()
        If ProjectManager.ProjectOpen Then
            Dim curproj = Globals.CurrentProject
            If IsNothing(SelectedNode.Parent) Then
                ' We clicked a class
                Dim classname As String = SelectedNode.Text
                Dim projfile = curproj.FileList.GetProjectFileByClassName(classname)
                Dim tab = MainForm.EditorTabManager.AddTab(projfile.FileName, projfile)
                tab.ScintillaEditor.Focus()
            Else
                ' We clicked a function or variable
                Dim classname As String = SelectedNode.Parent.Text
                Dim projfile = curproj.FileList.GetProjectFileByClassName(classname)

                Dim tab = MainForm.EditorTabManager.AddTab(projfile.FileName, projfile)

                Dim signature As String = SelectedNode.Text
                Dim func = projfile.UnrealClass.GetFunctionBySignature(signature)

                ' REMOVE THE NULL CHECKS
                ' omg bad code...
                ' ISNOTHING ISNOTHING ISNOTHING ISNOTHING ISNOTHING ISNOTHING 
                ' ISNOTHING ISNOTHING ISNOTHING ISNOTHING ISNOTHING ISNOTHING 
                ' ISNOTHING ISNOTHING ISNOTHING ISNOTHING ISNOTHING ISNOTHING 
                ' ISNOTHING ISNOTHING ISNOTHING ISNOTHING ISNOTHING ISNOTHING 
                ' ISNOTHING ISNOTHING ISNOTHING ISNOTHING ISNOTHING ISNOTHING

                If IsNothing(func) Then
                    ' It isn't a function
                    Dim var = projfile.UnrealClass.GetVariableBySignature(signature)
                    If Not IsNothing(var) Then
                        ' It's a variable!
                        tab.ScintillaEditor.GoTo.Line(var.LineNumber)
                        tab.ScintillaEditor.Caret.LineNumber = var.LineNumber - 1
                        tab.ScintillaEditor.Focus()
                    End If
                Else
                    tab.ScintillaEditor.GoTo.Line(func.LineNumber)
                    tab.ScintillaEditor.Caret.LineNumber = func.LineNumber - 1
                    tab.ScintillaEditor.Focus()
                End If
            End If
        End If
    End Sub


End Class
